Really fix `cargo test` and fix an OUT_DIR bug
authorAlex Crichton <alex@alexcrichton.com>
Sat, 1 Nov 2014 18:51:58 +0000 (11:51 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 5 Nov 2014 19:37:34 +0000 (11:37 -0800)
Assorted bug fixes discovered while migrating packages to using this build
command infrastructure.

src/cargo/ops/cargo_compile.rs
src/cargo/ops/cargo_rustc/custom_build.rs
src/cargo/ops/cargo_rustc/mod.rs
tests/test_cargo_compile_custom_build.rs

index 8b25881fe25fa515865970ab43bf833b01a3819f..6f47fe380b988b825252d733061894f7c623966b 100644 (file)
@@ -129,7 +129,7 @@ pub fn compile_pkg(package: &Package, options: &mut CompileOptions)
     };
 
     let targets = to_build.get_targets().iter().filter(|target| {
-        match env {
+        target.get_profile().is_custom_build() || match env {
             // doc-all == document everything, so look for doc targets
             "doc" | "doc-all" => target.get_profile().get_env() == "doc",
             env => target.get_profile().get_env() == env,
index b46505f1212a14e12484ce30907a524162b94b6e..727aebaec256789e6578d8b734414cebe7a49564 100644 (file)
@@ -81,7 +81,8 @@ pub fn prepare(pkg: &Package, target: &Target, cx: &mut Context)
     let pkg_name = pkg.to_string();
     let native_libs = cx.native_libs.clone();
     let all = (lib_name.clone(), pkg_name.clone(), native_libs.clone(),
-               script_output.clone());
+               script_output.clone(), old_build_output.clone(),
+               build_output.clone());
 
     try!(fs::mkdir(&script_output, USER_RWX));
 
@@ -165,9 +166,11 @@ pub fn prepare(pkg: &Package, target: &Target, cx: &mut Context)
             try!(fingerprint::prepare_build_cmd(cx, pkg, Some(target)));
     let dirty = proc(tx: Sender<String>) { try!(work(tx.clone())); dirty(tx) };
     let fresh = proc(tx) {
-        let (lib_name, pkg_name, native_libs, script_output) = all;
+        let (lib_name, pkg_name, native_libs, script_output,
+             old_build_output, build_output) = all;
         let new_loc = script_output.join("output");
         try!(fs::rename(&old_script_output.join("output"), &new_loc));
+        try!(fs::rename(&old_build_output, &build_output));
         let mut f = try!(File::open(&new_loc).map_err(|e| {
             human(format!("failed to read cached build command output: {}", e))
         }));
index dc9798e5cf173fb03098a92ce50f5dd461180006..a3a9c83d50fe3a699cb1e474ee320640a3f656c7 100644 (file)
@@ -175,7 +175,7 @@ fn compile<'a, 'b>(targets: &[&'a Target], pkg: &'a Package,
             // skipped entirely
             match pkg.get_manifest().get_links() {
                 Some(lib) => {
-                    if cx.native_libs.lock().contains_key_equiv(&lib) {
+                    if cx.native_libs.lock().contains_key_equiv(lib) {
                         continue
                     }
                 }
index 872d87116010e5ba4fa860f3dec8d28e3d24b024..52bd0b2e0fabf32000fdfab6f4296a207ba56fee 100644 (file)
@@ -450,6 +450,8 @@ test!(testing_and_such {
                 execs().with_status(0)
                        .with_stdout(format!("\
 {compiling} foo v0.5.0 (file://[..])
+{running} `[..]build-script-build`
+{running} `rustc [..] --crate-name foo [..]`
 {running} `rustc [..] --test [..]`
 {running} `[..]foo-[..]`
 
@@ -677,3 +679,76 @@ test!(build_cmd_with_a_build_cmd {
     -L [..]target -L [..]target[..]deps`
 ", compiling = COMPILING, running = RUNNING).as_slice()));
 })
+
+test!(out_dir_is_preserved {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            version = "0.5.0"
+            authors = []
+            build = "build.rs"
+        "#)
+        .file("src/lib.rs", "")
+        .file("build.rs", r#"
+            use std::os;
+            use std::io::File;
+            fn main() {
+                let out = os::getenv("OUT_DIR").unwrap();
+                File::create(&Path::new(out).join("foo")).unwrap();
+            }
+        "#);
+
+    // Make the file
+    assert_that(p.cargo_process("build").arg("-v"),
+                execs().with_status(0));
+    p.root().move_into_the_past().unwrap();
+
+    // Change to asserting that it's there
+    File::create(&p.root().join("build.rs")).write_str(r#"
+        use std::os;
+        use std::io::File;
+        fn main() {
+            let out = os::getenv("OUT_DIR").unwrap();
+            File::open(&Path::new(out).join("foo")).unwrap();
+        }
+    "#).unwrap();
+    p.root().move_into_the_past().unwrap();
+    assert_that(p.process(cargo_dir().join("cargo")).arg("build").arg("-v"),
+                execs().with_status(0));
+
+    // Run a fresh build where file should be preserved
+    assert_that(p.process(cargo_dir().join("cargo")).arg("build").arg("-v"),
+                execs().with_status(0));
+
+    // One last time to make sure it's still there.
+    File::create(&p.root().join("foo")).unwrap();
+    assert_that(p.process(cargo_dir().join("cargo")).arg("build").arg("-v"),
+                execs().with_status(0));
+})
+
+test!(output_separate_lines {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            version = "0.5.0"
+            authors = []
+            build = "build.rs"
+        "#)
+        .file("src/lib.rs", "")
+        .file("build.rs", r#"
+            fn main() {
+                println!("cargo:rustc-flags=-L foo");
+                println!("cargo:rustc-flags=-l foo");
+            }
+        "#);
+    assert_that(p.cargo_process("build").arg("-v"),
+                execs().with_status(0)
+                       .with_stdout(format!("\
+{compiling} foo v0.5.0 (file://[..])
+{running} `rustc build.rs [..]`
+{running} `[..]foo-[..]build-script-build`
+{running} `rustc [..] --crate-name foo [..] -L foo -l foo`
+", compiling = COMPILING, running = RUNNING).as_slice()));
+})